#include<iostream>

#include"protocol.hpp"

#include"socket.hpp"

#define SIZE 1024

#include<time.h>

void String_Parsing(Request& rq, const std::string& str)
{
    ssize_t left = str.find(' ');
    rq._a = std::stoi(str.substr(0, left));
    ssize_t pos = left;
    while(1)
    {
        if(str[pos] == ' ') pos++;
        else break;
    }
    rq._op = str[pos];
    ssize_t right = str.find(' ', pos);
    rq._b = std::stoi(str.substr(right));
}

int main()
{
    srand(time(NULL));
    sock sk;
    sk.my_socket();
    sk.my_connect("127.0.0.1", 8080);

    std::string opstr = "+-*/%=&";
    char buffer[SIZE] = {0};
    int cnt = 5;
    while(cnt--)
    {
        // char buffer[SIZE] = {0};
        sleep(1);
        Request rq;
        std::string message;
        rq._a = rand()%100;
        rq._b = rand()%100;
        rq._op = opstr[rand()%7];
        std::cout << rq._a << " " << rq._op << " " << rq._b << std::endl;

        // std::getline(std::cin, message);
        // String_Parsing(rq, message);
        message = "";
        rq.Serialization(message);
        std::string content = encode(message);
        write(sk.get_sockfd(), content.c_str(), content.size());
        ssize_t ret = read(sk.get_sockfd(), buffer, SIZE);
        if(ret > 0)
        {
            buffer[ret] = 0;
            std::string buf = buffer;
            Response rp;
            std::string tmp;
            decode(buf, tmp);
            rp.Deserialization(tmp);
            std::cout << rp._result << " " << rp._status_code << std::endl;
        }
    }
    
    return 0;
}